import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testclusterrolebinding:

    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrolebinding/1_get_clusterrolebinding_list.yaml'))
    def test_get_clusterrolebinding_list(self, clusterrolebindinginfo, get_env_info):
        # 获取clusterrolebinding列表接口
        baseUrl = get_env_info
        url = baseUrl + clusterrolebindinginfo['request']['url'].replace('{namespace}','default')
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrolebinding/2_create_clusterrolebinding.yaml'))
    def test_create_clusterrolebinding(self, clusterrolebindinginfo, get_env_info):
        # 创建clusterrolebinding接口
        baseUrl = get_env_info
        url = baseUrl + clusterrolebindinginfo['request']['url'].replace('{namespace}','default')
        method = clusterrolebindinginfo['request']['method']
        data = clusterrolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in clusterrolebindinginfo['name']:
            clusterrolebindingname=req.json()['metadata']['name']
            globals()['clusterrolebindingname']=clusterrolebindingname
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrolebinding/3_get_specific_clusterrolebinding.yaml'))
    def test_get_specific_clusterrolebinding(self, clusterrolebindinginfo, get_env_info):
        # 获取指定clusterrolebinding接口
        baseUrl = get_env_info
        url = baseUrl + clusterrolebindinginfo['request']['url'].replace('{namespace}','default').replace('{clusterrolebindingname}',globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in clusterrolebindinginfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrolebinding/4_update_clusterrolebinding.yaml'))
    def test_update_clusterrolebinding(self, clusterrolebindinginfo, get_env_info):
        # 更新指定clusterrolebinding接口
        baseUrl = get_env_info
        url = baseUrl + clusterrolebindinginfo['request']['url'].replace('{namespace}','default').replace('{clusterrolebindingname}',globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        clusterrolebindinginfo['request']['data'] = globals()['updatereqbody']
        data = clusterrolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrolebinding/5_update_section_clusterrolebinding.yaml'))
    def test_update_section_clusterrolebinding(self, clusterrolebindinginfo, get_env_info):
        # 部分更新指定clusterrolebinding的接口
        baseUrl = get_env_info
        url = baseUrl + clusterrolebindinginfo['request']['url'].replace('{namespace}','default').replace('{clusterrolebindingname}',globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        data = clusterrolebindinginfo['request']['data']
        header = clusterrolebindinginfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrolebinding/6_delete_specific_clusterrolebinding.yaml'))
    def test_delete_specific_clusterrolebinding(self, clusterrolebindinginfo, get_env_info):
        # 删除clusterrolebinding接口
        baseUrl = get_env_info
        url = baseUrl + clusterrolebindinginfo['request']['url'].replace('{namespace}','default').replace('{clusterrolebindingname}',globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']